<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class AddressController extends Controller
{
    public function getIndex(){
        $data = DB::table('shop_address') -> get();
        return view('admin/address/index',['data'=>$data]);
    }
    public function getDelete($id){
        $res = DB::table('shop_address') -> where('aid',$id) -> delete();
        if($res){
            return redirect('/admin/address/index') -> with('success','删除成功');
        }else{
            return back() -> withInput() -> with('error','删除失败');
        }
    }
    public function getEdit($id){
        $data = DB::table('shop_address') -> where('aid',$id) -> first();
        return view('/admin/address/edit',['data'=>$data]);
    }
    public function postUpdate($id,Request $request){
         $this->validate($request, [
            'ren' => 'required',
            'tel' => 'required',
            'province' => 'required',
            'country' => 'required',
            'town' => 'required',
            'xxdz' => 'required'
        ],[
            'ren.required' => '收件人必填',
            'tel.required' => '手机号必填',
            'province.required' => '省级必填',
            'country.required' => '市辖区必填',
            'town.required' => '县区必填',
            'xxdz.required' => '详细地址必填'
        ]);
         $a = $request -> input();
         $arr['ren'] = $a['ren'];
         $arr['tel'] = $a['tel'];
         $arr['dz'] = $a['province'].','.$a['country'].','.$a['town'];
         $arr['xxdz'] = $a['xxdz'];
         $res = DB::table('shop_address') -> where('aid',$id) -> update($arr);
         if($res){
            return redirect('admin/address/index') -> with('success','修改成功');
         }else{
            return back() -> withInput() -> with('error','修改失败');
         }
    }
}
